package org.javaboy.meeting.controller;

import org.javaboy.meeting.model.Department;
import org.javaboy.meeting.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("/admin")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 部门管理，显示有哪些部门
     * @param model
     * @return
     */
    @RequestMapping("/departments")
    public String departments(Model model){
         model.addAttribute("deps",departmentService.getAllDeps());
         return "departments";
    }

    /**
     * 添加部门信息
     * @param departmentname
     * @return
     */
    @RequestMapping("/adddepartment")
    public String adddepartment(String departmentname){

        departmentService.adddepartment(departmentname);
        return "redirect:/admin/departments";
    }

    /**
     * 删除部门
     * @param departmentid
     * @return
     */
    @RequestMapping("/deletedep")
    public String deletedep(Integer departmentid){
        departmentService.deletedep(departmentid);
        return "redirect:/admin/departments";
    }

    /**
     * 更新部门信息
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/updatedep")
    @ResponseBody
    public String updatedep(Integer id,String name){
        Integer result = departmentService.updatedep(id,name);
        if (result == 1){
            return "success";
        }
        return "error";
    }
}
